package com.example.springbootdemo;

import com.sun.tools.attach.*;
import com.sun.xml.internal.rngom.parse.host.Base;
import javassist.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StopWatch;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Scanner;

/**
 * Springboot启动时增加jvm参数： -javaagent:D:\learning\agentDemo\premainAgentDemo2\target\premainAgentDemo2-1.0-SNAPSHOT.jar=hello
 *
 * @author dingrz
 */
@SpringBootApplication
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        System.out.println("springboot开始启动，使用类加载器："+SpringbootDemoApplication.class.getClassLoader().getClass());
        SpringApplication.run(SpringbootDemoApplication.class, args);
        System.out.println("spingboot项目已启动.");



        //使用Javaassist创建一个类的字节码文件
        ClassPool pool = ClassPool.getDefault();
        //创建Student类
        CtClass cc = pool.makeClass("Student");
        try {
            //添加一个属性
            CtField f = CtField.make("public String code = \"0\";", cc);
            cc.addField(f);
            //定义one方法
            CtMethod method = CtNewMethod.make("public void one(){}", cc);
            //插入方法代码
            method.insertBefore("System.out.println(\"I'm a student,i am Jack\");");
            cc.addMethod(method);
            //保存生成的字节码
            cc.writeFile("D:\\learning\\agentDemo\\springbootDemo\\target\\classes\\com\\example\\springbootdemo\\model");
            System.out.println("新的字节码文件已经生成。");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
